/*
 * Created on 2005-3-24
 *
 */
package com.hd123.rumba.dbinit;

import java.io.File;

import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @author caili
 */

public class DbInit {

//  private Logger logger = Logger.getLogger(this.getClass());

  public static void main(String[] args) throws Exception {

    DbInit d = new DbInit();
    if (args.length != 2)
      printError();
    else if ("exportSchema".equals(args[0]))
      d.exportSchema(args[1]);
    // [2007-1-18 LeeXM] 相关功能被移动到安装/升级程序中。
    // else if ("createSchema".equals(args[0]))
    // d.createTables(args[1]);
    // [2007-3-13 LeeXM] 相关功能被移动到安装/升级程序中。
    // else if ("initData".equals(args[0]))
    // d.initData(args[1]);
    else
      printError();
  }

  private static void printError() {
    System.out.println("Arguments: [exportSchema] configuration-files");
    System.out.println("e.g. exportSchema /rumba.xml;/myrumba.xml");
    System.out.println("configuration files are seperated by ;");
  }

  /*
  public void createTables(String cfgFiles) throws Exception {
    logger.info("创建数据表...");

    new Configuration().configure(
        com.hd123.rumba.runtime.Configuration.getInstance().merge(cfgFiles).getBusinessDoc())
        .setProperty(Environment.HBM2DDL_AUTO, "create").buildSessionFactory().close();
    logger.info("数据表已创建.");
  }
  */

  public void exportSchema(String cfgFiles) throws Exception {
    Configuration config = new Configuration().configure(com.hd123.rumba.runtime.Configuration
        .getInstance().merge(cfgFiles).getBusinessDoc());
    SchemaExport se = new SchemaExport(config);
    se.setDelimiter(";");
    if (config.getProperty("hibernate.connection.driver_class").equals(
        "com.microsoft.jdbc.sqlserver.SQLServerDriver"))
      se.setDelimiter("\ngo");
    else if (config.getProperty("hibernate.connection.driver_class").equals(
        "oracle.jdbc.driver.OracleDriver"))
      se.setDelimiter(";");
    File file = new File("c:/temp");
    if (!file.exists())
      file.mkdir();
    se.setOutputFile("c:/temp/schema.sql");
    se.create(true, false);
  }

  /*
  public void importResource(String resourceName, String hintName) throws Exception {
    logger.info("导入/更新" + hintName + "定义" + resourceName + "...");
    ImpexXml impex = new ImpexXml();
    InputStream in = this.getClass().getResourceAsStream(resourceName);
    ImpexLog log = impex.doImport(in, resourceName, new UserMgr().getUserByLogin(new DomainMgr()
        .getRootDomain(), "admin"));
    logger.info(log.getMessage(Indicator.IMPORT));
    logger.info(hintName + "已导入/更新.");
  }
  */
}
